<?php


class AdminController extends BaseController {

	protected $layout='layouts.main';
	
	public function showAdmin()
	{
		$page='home';
		$this->layout->content = View::make('admin.home');
		$this->layout->side = View::make('admin.sidebar')->with('page',$page);
	}

	public function showPost()
	{
		$data['page']='post';
		$this->layout->content = View::make('admin.post');
		$this->layout->side = View::make('admin.sidebar',$data);
	}

	public function showCollection()
	{
		$data['page']='collection';
		$this->layout->content = View::make('admin.collection');
		$this->layout->side = View::make('admin.sidebar',$data);
	}
	
	public function showTest()
	{
		$data['page']='test';
		$this->layout->content = View::make('admin.test');
		$this->layout->side = View::make('admin.sidebar',$data);
	}

	public function settings()
	{
		$data['page']='settings';
		$this->layout->content = View::make('admin.settings');
		$this->layout->side = View::make('admin.sidebar',$data);
	}

	public function help()
	{
		$data['page']='help';
		$this->layout->content = View::make('admin.help');
		$this->layout->side = View::make('admin.sidebar',$data);
	}

	public function logout()
	{
		 Auth::logout();

    return Redirect::route('admin')
        ->with('flash_notice', 'You are successfully logged out.');
	}

	public function searchimage()
	{
		$keyword = Input::get('key');
		$limit = Input::get('limit');
		$keyword = str_replace(' ', '%20', $keyword);
		$data['images']=$this->getimage($keyword,$limit);
		echo json_encode($data);
		//$page='post';
		//$this->layout->content = View::make('admin.searchimage')->with('data',$data);
		//$this->layout->side = View::make('admin.sidebar')->with('page',$page);
		//echo '<pre>';
		//print_r($data);
		//echo '</pre>';
	}

	public function getimage($keyword,$limit)
	{
		$k=0;

		
		do {
			$url='https://ajax.googleapis.com/ajax/services/search/images?v=1.0&imgsz=huge&rsz=8&start='.$k.'&q='.$keyword;
			$json = file_get_contents($url);
			$json_res = json_decode($json, TRUE);
			foreach ($json_res['responseData']['results'] as $key => $res) {
				//echo '<a href="' . $res['url'] . '">' . $res['url'] . '</a>' . '</br>';
				$images[$k]['url']=$res['url'];
				$images[$k]['tbUrl']=$res['tbUrl'];
				$imges[$k]['width']=$res['width'];
				$images[$k]['height']=$res['height'];
				$delta = 24;
				$reduce_brightness = true;
				$reduce_gradients = true;
				$num_results = 5;
				$k++;
				if($k>=$limit) break;
			}
		} while ($k<=$limit);
		
		return $images;
	}

}